﻿using System.Collections;
using UnityEngine;

public partial class PlayerController : MonoBehaviour
{

    private int runAnimID = Animator.StringToHash("running");
    private int jumpAnimID = Animator.StringToHash("jumping");
    private int fallAnimID = Animator.StringToHash("falling");
    private int hitAnimID = Animator.StringToHash("hitting");

    private bool _playingHitAnim;


    void SwitchAnimation()
    {
        FaceDirection();

        RunningAnim();
        JumpingAnim();
        FaillingAnim();
        HittingAnim();
    }

    void FaceDirection()
    {
        if ((isFaceToRight && !sr.flipX) || (!isFaceToRight && sr.flipX))
        {
            return;
        }
        if (isFaceToRight)
        {
            sr.flipX = false;
        }
        else
        {
            sr.flipX = true;
        }
    }


    void RunningAnim()
    {
        if (isMoving)
        {
            anim.SetBool(runAnimID, true);
        }
        else
        {
            anim.SetBool(runAnimID, false);
        }
        
    }

    void JumpingAnim()
    {
        if (isJumping)
        {
            anim.SetBool(jumpAnimID, true);
        }
        else
        {
            anim.SetBool(jumpAnimID, false);
        }
    }

    void FaillingAnim()
    {
        if (isFalling)
        {
            anim.SetBool(fallAnimID, true);
        }
        else
        {
            anim.SetBool(fallAnimID, false);
        }
    }

    void HittingAnim()
    {
        if (isHitting)
        {
            if (!_playingHitAnim)
            {
                _playingHitAnim = true;
                anim.SetBool(hitAnimID, true);
                StartCoroutine(StopPlayHitAnim());
            }
        }
        else
        {
            anim.SetBool(hitAnimID, false);
        }
    }

    IEnumerator StopPlayHitAnim()
    {
        yield return new WaitForSeconds(0.3f);
        _playingHitAnim = false;
        isHitting = false;
        anim.SetBool(hitAnimID, false);
    }


    void StopAnim()
    {
        anim.SetBool(runAnimID, false);
        anim.SetBool(jumpAnimID, false);
        anim.SetBool(fallAnimID, false);
        anim.SetBool(hitAnimID, false);
    }

}
